var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    keywords: '',
    searchStatus: false,
    goodsList: [],
    page: 1,
    limit: 20
  },
  //事件处理函数
  closeSearch: function() {
    wx.navigateBack()
  },

  onLoad: function() {},
  
  getGoodsList: function() {
    let that = this;
    util.request(api.GoodsList, {
      keywords: that.data.keywords,
      page: that.data.page,
      limit: that.data.limit     
    }, 'POST').then(function(res) {
      if (res.errno === 0) {
        that.setData({
          searchStatus: true,
          goodsList: res.data.list
        });
      }
    });
  },

  getSearchResult(keywords) {
    if (keywords === '') {
      keywords = this.data.keywords;
    }
    this.setData({
      keywords: keywords,
      page: 1,
      goodsList: []
    });
    this.getGoodsList();
  },

  onKeywordConfirm(event) {
    this.getSearchResult(event.detail.value);
  }
})